package dto

import (
	"ginmt/models/model"

	"strings"
)

type item struct {
	Letter string   `json:"letter"`
	Data   []string `json:"data"`
}

func City(data []*model.CspMtRegional) []item {
	if len(data) == 0 {
		return []item{}
	}
	cityArr := make([]item, 0, 24)
	indexMap := make(map[string]int, 24)

	for _, value := range data {
		if v, ok := indexMap[value.CityPyFirst]; ok {
			cityArr[v].Data = append(cityArr[v].Data, value.City)
		} else {
			cityArr = append(cityArr, item{Letter: strings.ToUpper(value.CityPyFirst), Data: []string{value.City}})
			indexMap[value.CityPyFirst] = len(cityArr) - 1
		}
	}
	return cityArr
}
